package com.group18.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.group18.db.DBUtil;
import com.group18.entity.MemberUser;




/**
 * User dao类
 * @author Nicky
 *
 */
public class MemberUserDao extends BaseDao{
	
	/**
	 * 模糊搜索
	 * @param name
	 * @return
	 */
	public List<MemberUser> SelectAllByUsername(String username) {
		List<MemberUser> list = new ArrayList<MemberUser>();
		try {
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement("select * from member where username like ?");
			preparedStatement.setString(1, "%"+username+"%");
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				MemberUser u = new MemberUser();
				u.setId(resultSet.getInt("id"));
				u.setName(resultSet.getString("name"));
				u.setUsername(resultSet.getString("username"));
				u.setPassword(resultSet.getString("password"));
				u.setFamily_id(resultSet.getInt("family_id"));
				list.add(u);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closed();
		}
		return list;
	}
	
	/**
	 * 根据家庭id查询所有家庭成员
	 * @param family_id
	 * @return
	 */
	public int selectMemberByFamily_id(int family_id) {
		int i = 0;		
		try {
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement("select count(*) from member where family_id = ?");
			preparedStatement.setInt(1, family_id);
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				i = resultSet.getInt("count");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}
		return i;
	}
	
	/**
	 * 根据用户名查询id
	 * @param u
	 * @return
	 */
	public int selectIdByUsername(MemberUser u) {
		int id = 0;		
		try {
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement("select id from member where username = ?");
			preparedStatement.setString(1,u.getUsername());
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				id = resultSet.getInt("id");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}
		return id;
	}
	
	/**
	 * 根据id查用户数据
	 * @return
	 */
	public List<MemberUser> selectMemberByID(int id) {
		List<MemberUser> list = new ArrayList<>();
		try {
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement("select * from member where id = ?");
			preparedStatement.setInt(1, id);
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				MemberUser u = new MemberUser();
				u.setId(resultSet.getInt("id"));
				u.setName(resultSet.getString("name"));
				u.setFamily_id(resultSet.getInt("family_id"));
				u.setUsername(resultSet.getString("username"));
				u.setPassword(resultSet.getString("password"));
				list.add(u);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}	
		return list;
	}
	
	/**
	 * 根据查询用户名查询所属家庭
	 * @param u
	 * @return
	 */
	public String selectNameByUsername(MemberUser u) {
		String name = "";		
		try {
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement("select name from member where username = ?");
			preparedStatement.setString(1,u.getUsername());
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				name = resultSet.getString("name");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}
		return name;
	}
	
	/**
	 * 添加用户
	 * @param u
	 * @return	返回修改数据库条数0 失败 1成功
	 */
	public int addUser(MemberUser u) {
		int count = 0;
		try {
			connection = DBUtil.getConnection();
			String sql = "insert into member(name,family_id,username,password) values(?,?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, u.getName());
			preparedStatement.setInt(2, u.getFamily_id());
			preparedStatement.setString(3, u.getUsername());
			preparedStatement.setString(4, u.getPassword());
			count = preparedStatement.executeUpdate();
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			closed();
		}
		return count;
	}
	
	/**
	 * 修改用户数据
	 * @param u
	 * @return	修改数据条数
	 */
	public int upDataMember(MemberUser u) {
		int i = 0;
		try {
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement("update member set name = ?, family_id = ? , username = ? , password = ? where id = ?");
			preparedStatement.setString(1, u.getName());
			preparedStatement.setInt(2, u.getFamily_id());
			preparedStatement.setString(3, u.getUsername());
			preparedStatement.setString(4, u.getPassword());
			preparedStatement.setInt(5, u.getId());
			i = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}
	
	/**
	 * 根据家庭id删除属于这个家庭的所有成员
	 * @return
	 */
	public int deleteMemberByFamily_id(int family_id) {
		int i = 0;
		try {
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement("delete from member where family_id = ?");
			preparedStatement.setInt(1, family_id);
			i = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closed();
		}
		return i;
	}
	
	/**
	 * 根据家庭id修改家庭名
	 * @param family_id
	 * @return
	 */
	public int updateNameByFamily_id(MemberUser u) {
		int i = 0;
		try {
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement("update member set name = ? where family_id = ?");
			preparedStatement.setString(1, u.getName());
			preparedStatement.setInt(2, u.getFamily_id());
			i = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}
	
	/**
	 * 判断账号密码是否正确
	 * @param u
	 * @return	true 正确，false 不整确
	 */
	public boolean login(MemberUser u) {
		boolean flag = false;
		try {
			int count = 0;
			connection = DBUtil.getConnection();
			String sql = "select count(*) as count from member where username = ? and password = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, u.getUsername());
			preparedStatement.setString(2, u.getPassword());
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				count = resultSet.getInt("count");
			}
			if(count != 0) {
				flag = true;
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			closed();
		}
		return flag;
	}
	
	/**
	 * 判断用户名是否存在
	 * @param u
	 * @return	true 存在，false 不存在
	 */
	public boolean isExist(MemberUser u) {
		boolean flag = false;
		try {
			int count = 0;
			connection = DBUtil.getConnection();
			String sql = "select count(*) as count from member where username=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, u.getUsername());
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				count = resultSet.getInt("count");
			}
			if(count != 0) {
				flag = true;
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			closed();
		}
		return flag;
	}
	
	/**
	 * 查询某个家庭的所有成员
	 * @return
	 */
	public List<MemberUser> selectAllByName(MemberUser u) {
		List<MemberUser> uList = new ArrayList<>();
		
		try {
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement("select * from member where name = ?");
			preparedStatement.setString(1, u.getName());
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				MemberUser mu = new MemberUser();
				mu.setId(resultSet.getInt("id"));
				mu.setName(resultSet.getString("name"));
				mu.setFamily_id(resultSet.getInt("family_id"));
				mu.setUsername(resultSet.getString("username"));
				mu.setPassword(resultSet.getString("password"));
				uList.add(mu);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			closed();
		}
		return uList;
	}
	
	/**
	 * 查询所有用户
	 * @return
	 */
	public List<MemberUser> selectAllUser() {
		
		List<MemberUser> uList = new ArrayList<>();
		
		try {
			connection = DBUtil.getConnection();
			String sql = "select * from user";
			statement = connection.createStatement();
			resultSet =statement.executeQuery(sql);
			while(resultSet.next()) {
				MemberUser u = new MemberUser();
				u.setId(resultSet.getInt("id"));
				u.setName(resultSet.getString("name"));
				u.setFamily_id(resultSet.getInt("family_id"));
				u.setUsername(resultSet.getString("username"));
				u.setPassword(resultSet.getString("password"));
				uList.add(u);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			closed();
		}
		return uList;
	}
	
	/**
	 * 删除数据
	 * @param id
	 * 				要删除的id
	 * @return
	 * 				删除数据的条数
	 */
	public int deleteByID(int id) {
		int i = 0;
		try {
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement("delete from member where id = ?");
			preparedStatement.setInt(1, id);
			i = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closed();
		}
		return i;
	}
	
}
